<?php
/*fileName: 
 *internal version: 0001
 *version : 1.0.0 [programming] [official version]
 *author : HeShengyu
 *Date : 2014.08.27 
 *email: koolfer@163.com
 *end date:2014.08.27
 *http://www.cnblogs.com/ssnowy/
*/
class Cldir{
	public static function writeFileW1b($filename,$conntent){ 
	    $file=fopen($filename,"w+b");
		if(is_array($conntent)){
            foreach($conntent as $v){
				//将数组写入文件，每次追加一个数组的内容　
			    $ret =fwrite($file,$v);	
			}	
		}else{
		     $ret =fwrite($file,$conntent);
		}
	    fclose($file);
	    return  $ret;
    }	
	//Cldir::writeFile(
    //array('/a1koolfer/b/d/e/f/g/h/o',__FILE__)
    //,'abc.txt','文件内容'.date('Y-m-d H:i:s',time())
    //);
	public static function writeFile($path,$filename,$conntent='conntent'){
		if(count($path)==1){
	        $thePath=self::mkdir($path[0]);
		}
		if(count($path)==2){
			$thePath=self::mkdir($path[0],$path[1]);
		}
		//echo '{'.$thePath.'}';	
		$result=self::writeFileW1b($thePath.$filename,$conntent);
		echo '生成文件:'.$thePath.$filename.'</br>'; 
		echo $result?'ok</br>':'error</br>';
	}
	public static function mkdir($path,$__file__=''){
	    if(!empty($__file__)){
		    return self::mkdirNotTarget($path,$__file__);	
		}
		return self::mkdirs($path);	
	}
	public static function mkdirs($path){
    $target=preg_replace('/([A-Z]:[\/\\\\]){0,1}[\d\D]{0,}/','\\1',$path);
	if($target==''){
	    trigger_error("Cldir::mkdir,目标磁盘未定义",E_USER_ERROR);	
	}
    preg_match_all("/\/{1,}([^\/]{1,})/",$path,$matches);
	$stauts= $target;
		foreach($matches[1] as $v){
			$stauts=$stauts.($v."/");
			if(!file_exists($stauts) or !is_dir($stauts)){
				echo($stauts."<br>");
				echo mkdir($stauts,0777)
				?"<font color='#FF6600''>create dir ok</font><br>"
				:"<font color='#FF6600''>create dir error</font><br>";
			}else{
				echo $stauts.'目录已经存在</br>';	
			}
		}
	return $stauts;
    }	
	public static function mkdirNotTarget($path,$__file__){
		if(empty($path) or empty($__file__)){
		    trigger_error("Cldir::mkdirNotTarget,参数1,2都不能为空,
			参数1例如:/a/b参数2可以为__FILE__",E_USER_ERROR);	
			exit;	
		}
	     preg_match_all("/\/{1,}([^\/]{1,})/",$path,$matches);	
		 $stauts= self::getCurrentDir($__file__);
			foreach($matches[1] as $v){
				$stauts=$stauts.($v."/");
				if(!file_exists($stauts) or !is_dir($stauts)){
					echo($stauts."<br>");
					echo mkdir($stauts,0777)
					?"<font color='#FF6600''>create dir ok</font><br>"
					:"<font color='#FF6600''>create dir error</font><br>";
				}else{
					echo $stauts.'目录已经存在</br>';	
				}
			}
		return $stauts;	
	}
	public static function getCurrentDir($__file__){
		return preg_replace( array('/\/{1,}/'), array('/'),
	    str_replace( '\\','/' , dirname($__file__).'/' )   
		       );
		 	
	}
}
?>